package net.k3rnel.abugadro.gcodes;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.k3rnel.abugadro.ProjectsDB;
import net.k3rnel.abugadro.gcodes.beans.GoogleCode.GCode;
import net.k3rnel.abugadro.gcodes.beans.Issue.Issues;
import net.k3rnel.abugadro.gcodes.beans.Label.Labels;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.util.Log;


public class IssueFeedHandler extends DefaultHandler{

	private ContentResolver content;
	private ContentValues currentIssue;
	private StringBuilder builder;
	private int gcodeId;
	private String issueId;
	private boolean inEntry = false;

	// names of the XML tags
	static final String FEED = "feed";
	static final String ENTRY = "entry";
	static final String AUTHOR = "author";
	static final String OWNER = "owner";

	static final String PUB_DATE = "published";
	static final String UP_DATE = "updated";
	static final String TITLE = "title";
	static final String CONTENT = "content";
	static final String AUTHOR_NAME = "name";
	static final String AUTHOR_URI = "uri";
	static final String ISSUES_ID = "id";
	static final String ISSUES_LABEL = "label";
	static final String ISSUES_STARS = "stars";
	static final String ISSUES_STATE = "state";
	static final String ISSUES_STATUS = "status";

	public IssueFeedHandler(ContentResolver content, int gcodeId) {
		this.content = content;
		this.gcodeId = gcodeId;
	}

	@Override
	public void characters(char[] ch, int start, int length)
	throws SAXException {
		super.characters(ch, start, length);
		builder.append(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String name)
	throws SAXException {
		super.endElement(uri, localName, name);
		if (this.currentIssue != null){
			if (localName.equalsIgnoreCase(TITLE)&&inEntry){
				currentIssue.put(Issues.TITLE,builder.toString());
			} else if (localName.equalsIgnoreCase(CONTENT)&&inEntry){
				currentIssue.put(Issues.CONTENT,builder.toString());
			} else if (localName.equalsIgnoreCase(PUB_DATE)&&inEntry){
				currentIssue.put(Issues.PUBLISHED,builder.toString());
			} else if (localName.equalsIgnoreCase(UP_DATE)&&inEntry){
				currentIssue.put(Issues.UPDATED,builder.toString());
			} else if (localName.equalsIgnoreCase(AUTHOR_NAME)&&inEntry){
				currentIssue.put(Issues.AUTHOR,builder.toString());
			} else if (localName.equalsIgnoreCase(OWNER)&&inEntry){
				currentIssue.put(Issues.OWNER,builder.toString());
			} else if (localName.equalsIgnoreCase(ISSUES_ID)&&inEntry){
				issueId = builder.toString();
				currentIssue.put(Issues.ISSUE_ID,builder.toString());
			} else if (localName.equalsIgnoreCase(ISSUES_LABEL)&&inEntry){
				if(!ProjectsDB.getInstance().isLabelInDB(content, gcodeId, issueId, builder.toString())){
					ContentValues label = new ContentValues();
					label.put(Labels.ISSUE_ID,issueId);
					label.put(Labels.GCODE_ID,gcodeId);
					label.put(Labels.LABEL,builder.toString());
					content.insert(Labels.CONTENT_URI, label);
				}
			} else if (localName.equalsIgnoreCase(ISSUES_STARS)&&inEntry){
				currentIssue.put(Issues.STARS,builder.toString());
			} else if (localName.equalsIgnoreCase(ISSUES_STATE)&&inEntry){
				currentIssue.put(Issues.STATE,builder.toString());
			} else if (localName.equalsIgnoreCase(ISSUES_STATUS)&&inEntry){
				currentIssue.put(Issues.STATUS,builder.toString());
			} else if (localName.equalsIgnoreCase(ENTRY)&&inEntry){
				if(!ProjectsDB.getInstance().isIssueInDB(content, gcodeId, issueId)){
					Log.i("IssueFeedHandler","Created new Issue: "+gcodeId+"/"+issueId);
					content.insert(Issues.CONTENT_URI, currentIssue);
				}else{
					content.update(Issues.CONTENT_URI,currentIssue,Issues.ISSUE_ID + "='" + issueId + "' AND "+Issues.GCODE_ID + "= '"+gcodeId+"'",null);
					Log.i("IssueFeedHandler","Updated Issue: "+gcodeId+"/"+issueId);
				}
				currentIssue = new ContentValues();
				inEntry=false;
			}else if (localName.equalsIgnoreCase(FEED)){
				ContentValues cv = new ContentValues();
				cv.put(GCode._ID,gcodeId);
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // set the format to sql date time
				Date date = new Date();
				cv.put(GCode.LASTSYNC, dateFormat.format(date));
				content.update(GCode.CONTENT_URI,cv,GCode._ID + "='" + gcodeId + "'",null);
			}
			builder.setLength(0);	
		}
	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		builder = new StringBuilder();
	}

	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, name, attributes);
		if(localName.equalsIgnoreCase(FEED)){
			ContentValues cv = new ContentValues();
			cv.put(GCode._ID,gcodeId);
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // set the format to sql date time
			Date date = new Date();
			cv.put(GCode.LASTSYNC, dateFormat.format(date));

			content.update(GCode.CONTENT_URI,cv,GCode._ID + "='" + gcodeId + "'",null);
		}else if (localName.equalsIgnoreCase(ENTRY)){
			inEntry = true;
			this.currentIssue = new ContentValues();
			currentIssue.put(Issues.GCODE_ID,gcodeId);
		}
	}
}